<?php

namespace App\Http\Controllers\Applet\Order;

use App\Exceptions\ShopException;
use App\Http\Controllers\Applet\Shop\BaseShopController;
use App\Services\Applet\Order\ShopOrderService;
use Exception;
use Illuminate\Http\JsonResponse;

class ShopOrderController extends BaseShopController
{

    protected ShopOrderService $shopOrderService;

    /**
     * @throws Exception
     */
    public function __construct()
    {
        parent::__construct();
        $this->shopOrderService = $this->loadService('Applet\Order\ShopOrderService');
    }

    /**
     * 创建订单
     * @return JsonResponse
     * @throws ShopException
     */
    public function create(): JsonResponse
    {
        $rule = [
            'goodsId' => 'required|integer|min:1',
            'userAddressId' => 'required|integer|min:1',
            'quantity' => 'required|integer|min:1',
            'integral' => 'integer',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $data = $this->shopOrderService->create($this->params);

        return outputJsonResult($data);
    }


    /**
     * 购物车创建订单
     * @return JsonResponse
     * @throws Exception
     */
    public function cartCreate(): JsonResponse
    {
        $rule = [
            'cartList' => 'required|array',
            'cartList.*.id' => 'required:integer|min:1',
            'cartList.*.integral' => 'required:integer',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $data = $this->shopOrderService->cartCreate($this->params);
        return outputJsonResult($data);
    }

    /**
     * 取消订单
     * @return JsonResponse
     * @throws Exception
     */
    public function cancel(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->shopOrderService->cancel($this->params);
        return outputJsonResult();
    }

    /**
     * 删除订单
     * @return JsonResponse
     * @throws ShopException
     */
    public function delete(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->shopOrderService->delete($this->params);
        return outputJsonResult();
    }

    /**
     * 确认收货
     * @return JsonResponse
     * @throws ShopException
     */
    public function confirm(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->shopOrderService->confirm($this->params);
        return outputJsonResult();
    }

    /**
     * 订单列表
     * @return JsonResponse
     * @throws Exception
     */
    public function list(): JsonResponse
    {
        $list = $this->shopOrderService->list($this->params);
        return outputJsonResult($list);
    }

    /**
     * 订单详情
     * @return JsonResponse
     * @throws ShopException
     */
    public function detail(): JsonResponse
    {

        $rule = [
            'id' => 'required|integer|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $detail = $this->shopOrderService->detail($this->params);
        return outputJsonResult($detail);
    }
}
